package com.gxuwz.leave.dao;

import com.gxuwz.leave.entity.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class UserDaoImpl implements UserDao{
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insert(User entity) {
        String sql = "insert into sys_user (userid, fullname, password, telephone) values (?, ?, ?, ?)";
        List<Object> paramList = new ArrayList<>();
        if(entity==null){
            return;
        }else{
            paramList.add(entity.getUserid());
            paramList.add(entity.getFullname());
            paramList.add(entity.getPassword());
            paramList.add(entity.getTelephone());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public void delete(String userid) {
        String sql = "delete from sys_user where userid = ?";
        if(userid==null||"".equals(userid)){
            return;
        }else{
            jdbcTemplate.update(sql, userid);
        }
    }

    @Override
    public void update(User entity) {
        String sql = "update sys_user set ";
        List<Object> paramList = new ArrayList<>();
        if(entity==null){
            return;
        }else{
            if(entity.getPassword()!=null && !"".equals(entity.getPassword())){
                sql += "password = ?, ";
                paramList.add(entity.getPassword());
            }
            if(entity.getFullname()!=null && !"".equals(entity.getFullname())){
                sql += "fullname = ?, ";
                paramList.add(entity.getFullname());
            }
            if(entity.getTelephone()!=null && !"".equals(entity.getTelephone())){
                sql += "telephone = ?, ";
                paramList.add(entity.getTelephone());
            }

            // 移除多余的逗号和空格
            sql = sql.substring(0, sql.length() - 2);
            sql+="where userid = ?";
            paramList.add(entity.getUserid());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public List<User> query(User entity) {
        String sql="select * from sys_user where 1=1";
        List<User> list = new ArrayList<>();
        List<Object> paramList = new ArrayList<>();
        if(entity!=null){
            if(entity.getUserid()!=null && !"".equals(entity.getUserid())){
                sql += " and userid = ?";
                paramList.add(entity.getUserid());
            }
            if(entity.getPassword()!=null && !"".equals(entity.getPassword())){
                sql +=" and password = ?";
                paramList.add(entity.getPassword());
            }
            if(entity.getFullname()!=null && !"".equals(entity.getFullname())){
                sql += " and fullname like ?";
                paramList.add(entity.getFullname());
            }
        }
        list = jdbcTemplate.query(sql, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user = new User();
                user.setUserid(resultSet.getString("userid"));
                user.setPassword(resultSet.getString("password"));
                user.setFullname(resultSet.getString("fullname"));
                user.setTelephone(resultSet.getString("telephone"));
                return user;
            }
        }, paramList.toArray());
        return list;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
}
